﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveBackground : MonoBehaviour {

    private float Speed;
    public GameObject GameCtrl;
    public GameObject MoveTo;
    private GameObject Right, Left;
    private float Width;
    void Start()
    {
        Right = transform.FindChild("right").gameObject;
        Left = transform.FindChild("left").gameObject;
        Width = -Left.transform.position.x + Right.transform.position.x;
        Debug.Log(Width);
    }

    void getSpeed() {
        Speed = GameCtrl.GetComponent<GameCtrl>().MoveSpeed;
    }
    void Move() {
        transform.position = MoveTo.transform.position+new Vector3(Width,0,0);


    }
	void Update () {
        getSpeed();
        transform.Translate(new Vector2(-Speed * Time.deltaTime, 0));
        if (transform.position.x < -Width) {
            Move();
        }

    }
}
